﻿using FairyGUI;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 游戏玩家
/// </summary>
public class UIGamePlayer
{

	/// <summary>
	/// 玩家信息
	/// </summary>
	class UIPlayerInfo
	{
		/// <summary>
		/// 玩家详细信息
		/// </summary>
		class UIPlayerDetail
		{
			UIHeadpicImage _headpic;
			GTextField _name;
			GTextField _gold;

			public UIPlayerDetail (GComponent uiComp)
			{
				_headpic = new UIHeadpicImage (uiComp.GetChild ("头像").asCom);
				_name = uiComp.GetChild ("名字").asTextField;
				_gold = uiComp.GetChild ("金币").asTextField;
			}

			public void SetHeadpic (string headpic)
			{
				_headpic.SetHeadpic (headpic);
			}

			public void SetName (string name)
			{
				_name.text = name;
			}

			public void SetGold (string gold)
			{
				_gold.text = gold;
			}
		}

		GImage _master;
		GImage _dealer;
		GTextField _region;

		GComponent _waiting;
		GComponent _ready;
		UIPlayerDetail _playerDetail;

		GComponent _ofl1;
		GComponent _ofl2;


		public UIPlayerInfo (GComponent uiComp)
		{
			_master = uiComp.GetChild ("房主").asImage;
			_dealer = uiComp.GetChild ("庄").asImage;
			_region = uiComp.GetChild ("地区").asTextField;

			_waiting = uiComp.GetChild ("等待").asCom;
			_ready = uiComp.GetChild ("就绪").asCom;
			_playerDetail = new UIPlayerDetail (_ready);

			_ofl1 = uiComp.GetChild ("离线1").asCom;
			_ofl2 = uiComp.GetChild ("离线2").asCom;
		}

		bool offline {
			set {
				_ofl1.visible = _ofl2.visible = value;
			}
		}

		public void SetInfo (GameDataKernel.GamePlayer info)
		{
			if (info == null) {
				_master.visible = _master.enabled = false;
				_dealer.visible = _dealer.enabled = false;
				_region.text = "";
				_waiting.visible = _waiting.enabled = true;
				_ready.visible = _ready.enabled = false;
				offline = false;
			} else {
				_master.visible = _master.enabled = info.master;
				_dealer.visible = _dealer.enabled = info.dealer;
				_region.text = info.reagion;

				_playerDetail.SetHeadpic (info.headpic);
				_playerDetail.SetName (info.name);
				_playerDetail.SetGold (info.gold + "");

				_waiting.visible = _waiting.enabled = false;
				_ready.visible = _ready.enabled = true;

				offline = !info.online;
			}
		}
	}

	///////////////////////////////////////////////////////////////////////////

	protected class UIPlayerCards
	{
		protected GComponent _uiComp;
		protected Action<bool> _cardSel;

		public UIPlayerCards (GComponent uiComp)
		{
			_uiComp = uiComp;
		}

		public virtual void SetCards (List<int> cards)
		{

		}

		/// <summary>
		/// 获取选中的卡片。
		/// </summary>
		/// <returns>The sel cards.</returns>
		public virtual List<int> GetSelCards ()
		{
			return null;
		}

		public virtual void DeSelect ()
		{
		}

		public virtual void DoSelect (List<int> cards)
		{
		}

		/// <summary>
		/// 卡牌被选择
		/// </summary>
		/// <param name="action">Action.</param>
		public void OnCardSelected (Action<bool> action)
		{
			_cardSel = action;
		}
	}

	class UIPlayerCards_Slaver : UIPlayerCards
	{
		GImage[] _cards;

		public UIPlayerCards_Slaver (GComponent uiComp)
			: base (uiComp)
		{
			_cards = new GImage[6];
			for (int iCard = 1; iCard <= 6; ++iCard) {
				_cards [iCard - 1] = uiComp.GetChild ("牌" + iCard).asImage;
			}
		}

		public override void SetCards (List<int> cards)
		{
			if (cards == null) {
				_uiComp.visible = _uiComp.enabled = false;
				return;
			}
			_uiComp.visible = _uiComp.enabled = true;

			for (int iCard = 0; iCard < _cards.Length; ++iCard) {
				if (iCard < cards.Count) {
					_cards [iCard].visible = _cards [iCard].enabled = true;
				} else {
					_cards [iCard].visible = _cards [iCard].enabled = false;
				}
			}
		}
	}

	class UIPlayerCards_Master: UIPlayerCards
	{
		class UIPlayerCard : IComparable<UIPlayerCard>
		{
			GComponent _uiComp;
			GImage _card;
			GButton _cardBtn;
			Transition _anim;
			Transition _anim2;
			int _value;
			bool _sel;

			Action<bool> _selAction;

			public UIPlayerCard ()
			{
				_uiComp = UIPackage.CreateObject ("游戏界面", "牌").asCom;
				_card = _uiComp.GetChild ("牌").asImage;
				_anim = _uiComp.GetTransition ("出");
				_anim2 = _uiComp.GetTransition ("收");

				// 隐藏赖子标志
				GImage _img1 = _uiComp.GetChild ("赖1").asImage;
				_img1.visible = _img1.enabled = false;

				_cardBtn = _uiComp.GetChild ("按钮").asButton;
				_cardBtn.onClick.Add (() => {
					if (!_sel) {
						_anim.Play ();
						_selAction (true);
					} else {
						_anim2.Play ();
						_selAction (false);
					}
					_sel = !_sel;
				});
				_sel = false;
			}

			public void SetCard (int value)
			{
				_card.texture = UIPackage.GetItemAsset ("游戏界面", "" + value) as NTexture;
				_value = value;
			}

			public GComponent uiComp { get { return _uiComp; } }

			public int value { get { return _value; } }

			public bool selected {
				get { return _sel; } 
				set {
					if (_sel != value) {
						_cardBtn.onClick.Call ();
					}
				} 
			}

			public int CompareTo (UIPlayerCard obj)
			{
				return (_value % 100) - (obj.value % 100);
			}

			public void OnSelected (Action<bool> action)
			{
				_selAction = action;
			}
		}

		///////////////////////////////////////////////////////////////////////

		const int LEFT_BOUND = 329;
		const int RIGHT_BOUND = 1008;
		const int Y = 10;

		List<UIPlayerCard> _cards;

		public UIPlayerCards_Master (GComponent uiComp)
			: base (uiComp)
		{
			_cards = new List<UIPlayerCard> ();
		}

		public override void SetCards (List<int> cards)
		{
			if (cards == null) {
				_uiComp.visible = _uiComp.enabled = false;
				return;
			}
			_uiComp.visible = _uiComp.enabled = true;

			// 计算要添加的牌
			ArrayList cardsToAdd = new ArrayList ();
			Hashtable newTab = new Hashtable ();
			for (int iCard = 0; iCard < cards.Count; ++iCard) {
				int card = cards[iCard];
				if (newTab.ContainsKey (card)) {
					newTab [card] = (int)newTab [card] + 1;
				} else {
					newTab [card] = 1;
				}
			}
			foreach (int card in newTab.Keys) {
				int nCnt = 0;
				for (int jCard = 0; jCard < _cards.Count; ++jCard) {
					if (_cards [jCard].value == card) {
						nCnt += 1;
					}
				}
				int cntAdd = (int)newTab [card] - nCnt;
				while (cntAdd > 0) {
					cntAdd -= 1;
					cardsToAdd.Add (card);
				}
			}

			// 计算要删除的牌
			ArrayList cardsToDel = new ArrayList ();
			Hashtable hasTab = new Hashtable ();
			for (int iCard = 0; iCard < _cards.Count; ++iCard) {
				int card = _cards[iCard].value;
				if (hasTab.ContainsKey (card)) {
					hasTab [card] = (int)hasTab [card] + 1;
				} else {
					hasTab [card] = 1;
				}
			}
			foreach (int card in hasTab.Keys) {
				int nCnt = 0;
				for (int jCard = 0; jCard < cards.Count; ++jCard) {
					if (cards [jCard] == card) {
						nCnt += 1;
					}
				}
				int cntDel = (int)hasTab [card] - nCnt;
				while (cntDel > 0) {
					cntDel -= 1;
					cardsToDel.Add (card);
				}
			}

			// 添加牌
			if (cardsToAdd.Count > 0) {
				AddCards ((int[])cardsToAdd.ToArray (typeof(int)));
			}

			// 删除牌
			if (cardsToDel.Count > 0) {
				RemoveCards ((int[])cardsToDel.ToArray (typeof(int)));
			}

			// 重新布局
			LayoutCards ();
		}

		public override List<int> GetSelCards ()
		{
			List<int> selCards = new List<int> ();
			foreach (UIPlayerCard card in _cards) {
				if (card.selected) {
					selCards.Add (card.value);
				}
			}
			return selCards;
		}

		public override void DeSelect ()
		{
			foreach (UIPlayerCard card in _cards) {
				if (card.selected) {
					card.selected = false;
				}
			}
		}

		public override void DoSelect (List<int> cards)
		{
			DeSelect ();
			foreach (int card in cards) {
				SelectCard (card);
			}
		}

		void SelectCard (int cardVal)
		{
			foreach (UIPlayerCard card in _cards) {
				if (card.value == cardVal) {
					card.selected = true;
				}
			}
		}

		/// <summary>
		/// 创建新卡片
		/// </summary>
		/// <param name="cards"></param>
		void AddCards (int[] cards)
		{
			for (int iCard = 0; iCard < cards.Length; ++iCard) {
				UIPlayerCard card = new UIPlayerCard ();
				card.SetCard (cards [iCard]);
				card.OnSelected (_cardSel);
				_uiComp.AddChild (card.uiComp);
				_cards.Add (card);
			}
		}

		/// <summary>
		/// 删除卡片
		/// </summary>
		/// <param name="cards"></param>
		void RemoveCards (int[] cards)
		{
			for (int iCard = 0; iCard < cards.Length; ++iCard) {
				for (int jCard = 0; jCard < this._cards.Count; ++jCard) {
					if ((_cards [jCard] != null) && (_cards [jCard].value == cards [iCard])) {
						_cards [jCard].uiComp.RemoveFromParent ();
						_cards [jCard].uiComp.Dispose ();
						_cards [jCard].OnSelected (null);
						_cards.RemoveAt (jCard);
						break;
					}
				}
			}
		}

		void LayoutCards ()
		{
			// 排序卡片
			_cards.Sort ((a, b) => a.CompareTo (b));

			// 计算位置
			int rangW = RIGHT_BOUND - LEFT_BOUND;
			int cardsW = 109 * _cards.Count + 5 * (_cards.Count - 1);

			int curX = LEFT_BOUND + (rangW - cardsW) / 2;
			for (int iCard = 0; iCard < _cards.Count; ++iCard) {
				_cards [iCard].uiComp.x = curX;
				_cards [iCard].uiComp.y = Y;
				curX += 5 + 109;
			}
		}
	}

	public class UIPlayerPlayedCards
	{
		class UIPlayedCard
		{
			GComponent _uiComp;
			GImage _card;

			GImage _img1;
			GImage _img2;

			public UIPlayedCard (GComponent uiComp)
			{
				_uiComp = uiComp;
				_card = _uiComp.GetChild ("牌").asImage;

				_img1 = _uiComp.GetChild ("赖1").asImage;
			}

			public void SetCard (int value, bool showLaizi)
			{
				_card.texture = UIPackage.GetItemAsset ("游戏界面", "" + value) as NTexture;
				_img1.visible = _img1.enabled = showLaizi;
			}

			public bool visible {
				set {
					_uiComp.visible = value;
				}
			}
		}

		///////////////////////////////////////////////////////////////////

		GComponent _uiComp;
		UIPlayedCard[] _cards;

		public UIPlayerPlayedCards (GComponent uiComp)
		{
			_uiComp = uiComp;

			_cards = new UIPlayedCard[6];
			for (int i = 1; i <= 6; ++i) {
				_cards [i - 1] = new UIPlayedCard (uiComp.GetChild ("出牌" + i).asCom);
			}
		}

		public void SetCards (int[] cards, int type, int point)
		{
			for (int i = 0; i < _cards.Length; ++i) {
				if (i < cards.Length) {
					int cardVal = cards [i];

					bool isLaizi = false;
					if (cardVal == 1) {
						switch (type) {
						case GameEnum.CardType.TYPE_PAIR:
						case GameEnum.CardType.TYPE_BOMB:
							cardVal = point;
							break;
						case GameEnum.CardType.TYPE_ORDER:
							cardVal = (point - cards.Length + 1) + i;
							break;
						case GameEnum.CardType.TYPE_ORDER_PAIR:
							cardVal = (point - (cards.Length / 2) + 1) + i / 2;
							break;
						}
						if (cardVal < 100)
							cardVal += 100;
						isLaizi = true;
					}

					_cards [i].SetCard (cardVal, isLaizi);
					_cards [i].visible = true;
				} else {
					_cards [i].visible = false;
				}
			}
		}

		public bool visible {
			set { 
				_uiComp.visible = _uiComp.enabled = value;
			}
		}
	}

	class UIChat
	{
		GComponent _ui;
		GTextField _text;

		public UIChat (GComponent uiComp)
		{
			_ui = uiComp;
			_text = uiComp.GetChild ("聊天").asTextField;
		}

		public bool visible {
			set {
				_ui.visible = _ui.enabled = value;
			}
		}

		public string text {
			set {
				_text.text = value;
			}
		}
	}

	///////////////////////////////////////////////////////////////////////////

	GComponent _uiComp;
	GImage _ready;
	UIPlayerInfo _playerInfo;
	UIPlayerCards _cards;
	UIPlayerPlayedCards _playedCards;
	UIChat _chat;
	Transition _showChat;
	GImage _voice;

	public UIGamePlayer (GComponent uiComp, bool master = false)
	{
		_uiComp = uiComp;

		_ready = uiComp.GetChild ("就绪").asImage;
		_playerInfo = new UIPlayerInfo (uiComp.GetChild ("玩家").asCom);
		if (master) {
			_cards = new UIPlayerCards_Master (uiComp.GetChild ("牌组").asCom);
		} else {
			_cards = new UIPlayerCards_Slaver (uiComp.GetChild ("牌组").asCom);
		}

		_playedCards = new UIPlayerPlayedCards (uiComp.GetChild ("出牌").asCom);
		_playedCards.visible = false;

		_chat = new UIChat (uiComp.GetChild ("聊天").asCom);
		//_chat.visible = false;
		_showChat = uiComp.GetTransition ("隐藏聊天");

		// 隐藏语音
		_voice = uiComp.GetChild ("喇叭").asImage;
		_voice.visible = false;

		SetInfo (null);
	}

	public void SetInfo (GameDataKernel.GamePlayer info)
	{
		if (info == null) {
			_ready.visible = false;
			_ready.enabled = false;
			_playerInfo.SetInfo (info);
			_cards.SetCards (null);
		} else {
			_ready.visible = !GameDataKernel.inst.playing && info.ready;
			_ready.enabled = _ready.visible;
			_playerInfo.SetInfo (info);
			_cards.SetCards (info.cards);
		}
	}

	protected UIPlayerCards cards { get { return _cards; } }

	public void ShowPlayedCards (int[] cards, int type, int point)
	{
		_playedCards.SetCards (cards, type, point);
		_playedCards.visible = true;
		//GameController.inst.proceduar.paused = true;

		//TimerController.inst.CallAfter(2.0f, param => {
		//	_playedCards.visible = false;
		//	GameController.inst.proceduar.paused = false;
		//});
	}

	public void ShowChatMessage (string message)
	{
		_chat.text = message;
		_showChat.Play ();
	}

	public bool voiceVisible {
		set {
			_voice.visible = _voice.enabled = value;
		}
	}

	public void HiddPlayedCards ()
	{
		_playedCards.visible = false;
	}
}
